package jjn.carl.greedy;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author Jjn
 * @since 2023/8/1 12:35
 */
public class LeetCode135 {
    public int candy(int[] ratings) {
        int[] candyNum = new int[ratings.length];
        Arrays.fill(candyNum, 1);
        for (int i = 1; i < ratings.length; i++) {
            if (ratings[i] > ratings[i - 1]) {
                candyNum[i] = candyNum[i - 1] + 1;
            }
        }
        for (int i = ratings.length - 2; i >= 0; i--) {
            if (ratings[i + 1] < ratings[i]) {
                candyNum[i] = Math.max(candyNum[i], candyNum[i + 1] + 1);
            }
        }
        int total = 0;
        for (int candy : candyNum) {
            total += candy;
        }
        return total;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int total = scanner.nextInt();
        int[] ratings = new int[total];
        for (int i = 0; i < total; i++) {
            ratings[i] = scanner.nextInt();
        }
        int candy = new LeetCode135().candy(ratings);
        System.out.println(candy);
    }
}
